package com.lzq.model.proxy.cglibproxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author : modify
 * @version : v1.0
 * @description :
 * @createTime : 2022/11/22 13:53
 * @updateUser : modify
 * @updateTime : 2022/11/22 13:53
 * @updateRemark :
 */
public class ProxyFactory implements MethodInterceptor {

    private Station station = new Station();

    public Station getProxyObject() {
        Enhancer enhancer = new Enhancer();
        //设置父类的字节码对象
        enhancer.setSuperclass(station.getClass());
        enhancer.setCallback(this);
        Station obj = (Station) enhancer.create();
        return obj;
    }

    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        Object invoke = methodProxy.invokeSuper(o, objects);
        return invoke;
    }
}

class Test {
    public static void main(String[] args) {
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.getProxyObject().sell();
        System.out.println(proxyFactory.getProxyObject().getClass());
        while (true) {

        }
    }
}